﻿using MiniExcelLibs;
using System.IO;
using System.Windows;
using unvell.ReoGrid.IO;

namespace Wpf使用表格
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //获得当前目录的路径
            string currentDirectory = Directory.GetCurrentDirectory();

            //合并当前目录与Excel文件名
            var path = Path.Combine(currentDirectory, "test.csv");
            var path2 = Path.Combine(currentDirectory, "test2.csv");

            //保存数据
            //List<Student> studentList = new List<Student>() {
            //       new Student() { Name = "小东", Home = "New York", Age = 25 },
            //       new Student() { Name = "小西", Home = "London", Age = 22 },
            //       new Student (){ Name = "小南", Home = "Paris", Age = 28 },
            //       new Student (){ Name = "小北", Home = "Tokyo", Age = 24 },
            //       new Student() { Name = "小王", Home = "Berlin", Age = 26 }
            //  };


            //List<Student> studentList = new List<Student>();
            //for (int i = 0; i < 10000; i++)
            //{
            //    studentList.Add(new Student() { Name = "小东" + i, Home = "New York" + i, Age = i });
            //}

            //if (File.Exists(path))
            //{
            //    MiniExcel.Insert(path, studentList, excelType: ExcelType.CSV);
            //}
            //else
            //{
            //    MiniExcel.SaveAs(path, studentList, excelType: ExcelType.CSV, overwriteFile: false);
            //}



            //读取数据
            var rows = MiniExcel.Query<Student>(path);


            //展示到前端
            //var workbook = reoGridControl;
            try
            {
                using (Stream stream = File.OpenRead(path))
                {
                    reoGridControl.Load(stream, FileFormat.CSV);
                }
            }
            catch (Exception ex)
            {
                //_VM.ShowInfo($"载入用户权限表异常：{ex}");
            }
        }



        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            string currentDirectory = Directory.GetCurrentDirectory();
            var path2 = Path.Combine(currentDirectory, "test2.xlsx");
            reoGridControl.Save(path2);
        }
    }
}